import { encryptBase64, encryptWithAes, generateAesKey } from "../utils/crypto";
import { encrypt } from "../utils/jsencrypt";
import { request } from "../utils/request";
import { Response } from "./common";
import type {
  GetCaptchaApiResponse,
  LoginApiParams,
  LoginApiResponse,
  GetUserInfoApiResponse,
  GetMenusApiResponse,
} from "./system.d";

// 获取验证码
export const getCaptchaApi = (): Response<GetCaptchaApiResponse> => {
  return request.get("/auth/code");
};

// 登录
export const loginApi = (data: LoginApiParams): Response<LoginApiResponse> => {
  const aesKey = generateAesKey();
  const headers = {
    "encrypt-key": encrypt(encryptBase64(aesKey)),
    isencrypt: true,
  };
  return request.post(
    "/auth/login",
    encryptWithAes(JSON.stringify(data), aesKey),
    { headers },
  );
};

// 获取用户信息
export const getUserInfoApi = (): Response<GetUserInfoApiResponse> => {
  return request.get("/system/user/getInfo");
};

// 获取菜单
export const getMenusApi = (): Response<GetMenusApiResponse> => {
  return request.get("system/menu/getRouters");
};
